
import { globalUtils } from '../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import uiData from 'ui-data';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewScope('menu-management', function (scope) {
  var jqNewMenuDialog;
  var jqNewMenuForm;
  var dataModel;

  var menuValidationRule = {
    rules: {
      menuCode: {
        required: true,
        maxlength: 50,
        customCode: true
      },
      menuName: {
        required: true,
        maxlength: 20
      },
      menuIcon: {
        maxlength: 50
      },
      menuPath: {
        required: true,
        maxlength: 500
      },
      menuOrder: {
        required: true,
        digits: true
      },
      remark: {
        maxlength: 200
      }
    },
    messages: {
      menuCode: {
        required: '${t("ui.menu.common.validation.menuCodeRequired")}'
      },
      menuName: {
        required: '${t("ui.menu.common.validation.menuNameRequired")}'
      },
      menuPath: {
        required: '${t("ui.menu.common.validation.menuPathRequired")}',
      },
      menuOrder: {
        required: '${t("ui.menu.common.validation.menuOrderRequired")}'
      }
    }
  };

  scope.showNewMenuDialog = function (view, tempateId, parentMenu) {
    if (globalUtils.isNullOrUndefined(jqNewMenuDialog)) {
      jqNewMenuDialog = view.$ui('newMenuDialog');
      jqNewMenuForm = jqNewMenuDialog.find('form');
      dataModel = view.getDataModel();

      // 添加校验规则
      jqNewMenuForm.validate(menuValidationRule);

      // 添加对话框事件
      addDialogEvent(view);
    }

    // 显示弹出框
    uiUtils.dialog.showModal(jqNewMenuDialog, view, {
      backdropParent: view.getViewElement(),
      onModalShow: function () {
        var parentId = null;
        var parentName = null;

        if (!globalUtils.isNullOrUndefined(parentMenu)) {
          parentId = parentMenu.id;
          parentName = parentMenu.menuName;
        }

        // 初始对话框数据
        dataModel.setData('new.*', {
          'new.templateId': tempateId,
          'new.parentId': parentId,
          'new.parentName': parentName,
        });

        uiUtils.view.checkFormDataChangeWhenLeave(scope, getDialogData);
      },
      onModalHide: function () {
        uiUtils.common.resetValidation(jqNewMenuForm);
      }
    });
  };

  /**
   * 添加对话框事件
   * @param {View} view 
   */
  function addDialogEvent(view) {
    jqNewMenuDialog.on(uiUtils.event.MODAL_HIDE, function () {
      uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
    });

    // 点击"新建菜单"对话框的保存按钮后调用接口保存菜单
    jqNewMenuDialog.on(uiUtils.event.CLICK, uiUtils.dialog.BTN_CONFIRM, function () {
      var checkResult = jqNewMenuForm.valid();
      if (!checkResult) {
        return;
      }

      var saveData = dataModel.getData('new.*');
      saveData = uiData.utils.unprefix(saveData, 'new.');

      // 调用保存记录的接口
      view.callService('api/menu/menu/saveMenu', saveData)
        .waitResult(function (result) {
          if (result.success) {
            uiUtils.dialog.hideModal(jqNewMenuDialog);
            scope.refreshTreeMenuStructure();
            Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
          } else {
            Pure.app.showError(result.message);
          }
        });
    });
  }

  // 返回对话框的数据
  function getDialogData() {
    return dataModel.getData('new.*');
  }

});

/* SOURCE-CODE-END */


